package com.abing.starry.strategy.login;

import com.abing.starry.common.ErrorCode;
import com.abing.starry.exception.BusinessException;
import com.abing.starry.model.request.user.UserLoginRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author CaptainBing
 * @Date 2024/8/28 9:37
 * @Description
 */
@Component
@RequiredArgsConstructor
public class LoginStrategyExecutor {


    private final List<LoginStrategy> loginStrategy;

    public String doLogin(UserLoginRequest userLoginRequest) {

        String loginType = userLoginRequest.getLoginType();
        LoginTypeEnum loginTypeEnum = LoginTypeEnum.valueOf(loginType.toUpperCase());

        for (LoginStrategy strategy : loginStrategy) {

            if (strategy.getClass().isAnnotationPresent(LoginTypeConfig.class)) {
                LoginTypeConfig annotation = strategy.getClass().getAnnotation(LoginTypeConfig.class);
                if (annotation.loginType().equals(loginTypeEnum)) {
                    return strategy.doLogin(userLoginRequest);
                }
            }

        }
        throw new BusinessException(ErrorCode.PARAMS_ERROR,"登录类型不存在");
    }


}
